package com.gexiaobao.pigeon.ui.fragment.mine.tools.query

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.databinding.FragmentRaceByNameDetailBinding
import com.gexiaobao.pigeon.ui.adapter.SearchRaceByNameDetailAdapter
import com.gexiaobao.pigeon.viewmodel.MineToolsViwModel
import me.hgj.jetpackmvvm.ext.nav

/**
 * @Author      : hxw
 * @Date        : 2023/8/7 15:48
 * @Describe    : 组织方比赛详情
 */
class FragmentRaceByNameDetail : BaseFragment<MineToolsViwModel, FragmentRaceByNameDetailBinding>() {

    private var title = ""
    private var raceName = ""
    private val mAdapter: SearchRaceByNameDetailAdapter by lazy { SearchRaceByNameDetailAdapter(arrayListOf()) }

    override fun initView(savedInstanceState: Bundle?) {
        title = arguments?.getString("name").toString().trim()
        raceName = arguments?.getString("raceName").toString().trim()

        mDatabind.includeBar.toolbar.initClose(title) {
            nav().navigateUp()
        }

        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.initFooter {
                /** 触发加载更多时请求数据 */
                showLoadingAntiShake()
                mViewModel.searchRaceDetail(false, title, raceName)
            }
        }
        mDatabind.layoutList.swipeRecyclerView.adapter = mAdapter

        /** 下拉刷新 */
        mDatabind.layoutList.swipeRefreshLayout.init {
            showLoadingAntiShake()
            mViewModel.searchRaceDetail(true, title, raceName)
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        showLoading(getString(R.string.loading))
        mViewModel.searchRaceDetail(true, title, raceName)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        mViewModel.searchRaceByNameDetailResult.observe(viewLifecycleOwner) {
            dismissLoadingAntiShake()
            if (mDatabind.layoutList.swipeRefreshLayout.isRefreshing) {
                mDatabind.layoutList.swipeRefreshLayout.isRefreshing = false
            }
            mDatabind.data = it
            mAdapter.setList(it.list)
            mAdapter.notifyDataSetChanged()
        }
    }
}